package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.pojo.Manager;
import com.ssm.pojo.Msg;
import com.ssm.pojo.SManager;
import com.ssm.pojo.Stu;
import com.ssm.service.ManagerService;
import com.ssm.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ManagerController {

    @Autowired
    ManagerService managerService;

    @Autowired
    StuService stuService;

    @RequestMapping(value = "/showStuss", method = RequestMethod.GET)
    @ResponseBody
    public Msg showStus(@RequestParam(value = "pn", defaultValue = "1") int pn,
                        @RequestParam(value = "select_value", defaultValue = "1") int value,
                        @RequestParam(required = false) String find_value) {
        /*
         * 在查询之前只需要调用，传入页码。以及每页的大小
         * */
        if (find_value == "undefined") {
            find_value = "";
        }
        PageHelper.startPage(pn, 5);
        if (value == 1) {
            List<Stu> list = stuService.queryAll(null);
            PageInfo page = new PageInfo(list, 5);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else if (value == 2) {
            List<Stu> list = stuService.find_name(find_value,null);
            PageInfo page = new PageInfo(list);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else if (value == 3) {
            List<Stu> list = stuService.find_cdorm(find_value);
            PageInfo page = new PageInfo(list, 5);
            return Msg.success().add("pageInfo", page).add("selectvalue", value).add("find_v", find_value);
        } else {
            return Msg.fail().add("value", value);
        }
    }

    @PostMapping("/update_pw")
    @ResponseBody
    public Msg updates(String amount, String password, HttpSession session){
        try {
            managerService.update_pw(amount,password);
        } catch (Exception e) {
            return Msg.fail();
        }
        session.setAttribute("manager",managerService.query(amount));
        return Msg.success();
    }

    @PostMapping("/update")
    @ResponseBody
    public Msg update(Manager manager, HttpSession session) {
        try {
            managerService.updateManager(manager);
            session.setAttribute("manager",managerService.query(manager.getAmount()));
        } catch (Exception e) {
            return Msg.fail();
        }
        return Msg.success();
    }
}
